home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / write_nrif.pro < prev    next >
Text File  |  1997-07-08  |  6KB  |  184 lines

  1. ; $Id: write_nrif.pro,v 1.4 1996/12/18 01:21:12 ali Exp $
  2.  
  3. PRO WRITE_NRIF, FILE, IMAGE, R, G, B
  4.  
  5. ;+
  6. ; NAME:
  7. ;    WRITE_NRIF
  8. ;
  9. ; PURPOSE:
  10. ;    Write an IDL image and color table vectors to an
  11. ;    NCAR Raster Interchange Format (NRIF) rasterfile.
  12. ;
  13. ; CATEGORY:
  14. ;    Input/Output.
  15. ;
  16. ; CALLING SEQUENCE:
  17. ;    WRITE_NRIF, File, Image, [R, G, B]
  18. ;
  19. ; INPUTS:
  20. ;    File:    A string containing the name of the rasterfile to write.
  21. ;
  22. ;    Image:    The byte array to be output.  If Image is dimensioned [n,m] an
  23. ;        8-bit NCAR Raster File with color tables is output.  If Image 
  24. ;        is dimensioned [3,n,m], a 24-bit NCAR Raster File is output, 
  25. ;        where each byte triple represents the red, green, and blue 
  26. ;        intensities at [n,m] on a scale from 0 to 255.  In either 
  27. ;        case, IMAGE must be a byte array.  The NRIF image will be 
  28. ;        rendered from bottom to top, in accordance with IDL standards,
  29. ;        so the !ORDER variable should not be changed from its default 
  30. ;        value of zero.
  31. ;
  32. ; OPTIONAL INPUT PARAMETERS:
  33. ;      R, G, B:    The Red, Green, and Blue color vectors to be used as a color
  34. ;        table with 8-bit images.  If color vectors are supplied, they
  35. ;        are included in the output (8-bit images only).  If color 
  36. ;        vectors are not supplied, the color vectors established by 
  37. ;        LOADCT or PALETTE are included in the output.  If LOADCT or 
  38. ;        PALETTE have not yet been used to define color vectors, 
  39. ;        "LOADCT, 0" is called to load the standard grayscale color
  40. ;        table.
  41. ;
  42. ;        This routine does not recognize color vectors loaded directly 
  43. ;        using TVLCT, so if a custom color table is desired and it is 
  44. ;        not convenient to use PALETTE, include the R, G, and B vectors
  45. ;        that were used to create the color table.
  46. ;
  47. ; OUTPUTS:
  48. ;    No explicit outputs.  The specified File will contain header 
  49. ;    information, color vectors (8-bit images only), and the image, in
  50. ;    NCAR Raster Interchange Format (NRIF).
  51. ;
  52. ; COMMON BLOCKS:
  53. ;    COLORS:  The IDL color table common block.
  54. ;
  55. ; SIDE EFFECTS:
  56. ;    If R, G, and B aren't supplied and color tables haven't been previously
  57. ;    established by LOADCT or PALETTE, this routine calls "LOADCT, 0" to 
  58. ;    load the standard gray scale color table.
  59. ;
  60. ; RESTRICTIONS:
  61. ;    This routine only writes 8 or 24-bit deep rasterfiles of types
  62. ;    "Indexed Color" (for 8-bit) and "Direct Color integrated" for 24-bit.
  63. ;    The color map is included only for 8-bit files.
  64. ;
  65. ; FURTHER INFORMATION:
  66. ;    See the document "NCAR Raster Interchange Format and TAGS Raster
  67. ;    Reference Manual", available from the Scientific Computing Division,
  68. ;    National Center for Atmospheric Research, Boulder, CO, 80307-3000,
  69. ;    for the structure of NCAR Raster Interchange Format (NRIF) files.
  70. ;
  71. ; MODIFICATION HISTORY:
  72. ;    Written February, 1991 by Stan Solomon, LASP, University of Colorado.
  73. ;    (Adapted from the WRITE_SRF procedure.)
  74. ;-
  75. common colors, r_orig, g_orig, b_orig, r_curr, g_curr, b_curr
  76.  
  77. ; Check the arguments:
  78. on_error, 1
  79. n_params = n_params()
  80. if ((n_params ne 2) and (n_params ne 5))then $
  81.   message, "usage: WRITE_NRIF, file, image, [r, g, b]"
  82.  
  83. ; Check that image has the required attributes:
  84. img_size = size(image)
  85. if ( (img_size[0] ne 2) and (img_size[0] ne 3) ) then  $
  86.   message, 'Image must be a matrix.'
  87. if ( (img_size[0] eq 3) and (img_size[1] ne 3) ) then $
  88.   message, '24 bit images must be dimensioned (3,n,m)'
  89. if ( (img_size[0] eq 2) and (img_size[3] ne 1) ) then $
  90.   message, 'Image must be a byte array.'
  91. if ( (img_size[0] eq 3) and (img_size[4] ne 1) ) then $
  92.   message, 'Image must be a byte array.'
  93.  
  94. ; Determine if this is an 8-bit or 24-bit image:
  95. if (img_size[0] eq 3) then begin
  96.   depth = 24L
  97.   cols = img_size[2]
  98.   rows = img_size[3]
  99. endif else begin
  100.   depth = 8L
  101.   cols = img_size[1]
  102.   rows = img_size[2]
  103. endelse
  104.  
  105. ; Load color vectors into color map if supplied, otherwise use loadct vectors:
  106. if (n_params eq 5) then begin
  107.   r_size = size(r)
  108.   g_size = size(g)
  109.   b_size = size(b)
  110.   if ((r_size[0] + g_size[0] + b_size[0]) ne 3) then $
  111.     message, "R, G, & B must all be 1D vectors."
  112.   if ( (r_size[1] ne g_size[1]) or (r_size[1] ne b_size[1]) ) then $
  113.     message, "R, G, & B must all have the same length."
  114.   map_len = r_size[1] * 3L
  115.   rmap = byte(r)
  116.   gmap = byte(g)
  117.   bmap = byte(b)
  118. endif else begin
  119.   if (n_elements(r_orig) eq 0) then loadct, 0
  120.   tmp = size(r_orig)
  121.   map_len = tmp[1] * 3L
  122.   rmap = byte(r_orig)
  123.   gmap = byte(g_orig)
  124.   bmap = byte(b_orig)
  125. endelse
  126.  
  127. ; Construct header:
  128. magic= 'NRIF'
  129. flags= 2L
  130. width= cols
  131. height= rows
  132. cmtlen= 0L
  133. dev= 0L
  134. devlen= 0L
  135. ibits= 8L
  136. cbits= 8L
  137. ncolor= map_len/3L
  138. if (depth eq 8) then begin
  139.   encoding= 2L
  140.   enclen= map_len+12L
  141. endif
  142. if (depth eq 24) then begin
  143.   encoding= 4L
  144.   enclen= 4L
  145. endif
  146.  
  147. ; If this is a small-endian machine, reverse the byte order of longword
  148. ; integers in the header:
  149. test = byte(1L,0,4)
  150. if (test[0] eq 1b) then begin
  151.   flags = swap_endian(flags)
  152.   width = swap_endian(width)
  153.   height = swap_endian(height)
  154.   cmtlen = swap_endian(cmtlen)
  155.   dev = swap_endian(dev)
  156.   devlen = swap_endian(devlen)
  157.   encoding = swap_endian(encoding)
  158.   enclen = swap_endian(enclen)
  159.   ibits = swap_endian(cbits)
  160.   ncolor = swap_endian(ncolor)
  161.   cbits = swap_endian(cbits)
  162. endif
  163.  
  164. ; Write header:
  165. openw, unit, file, /stream, /get_lun
  166. if (depth eq 8) then begin
  167.   writeu, unit, magic, flags, width, height, cmtlen, dev, devlen, $
  168.     encoding, enclen, ibits, ncolor, cbits
  169.   writeu, unit, rmap, gmap, bmap
  170. endif
  171. if (depth eq 24) then begin
  172.   writeu, unit, magic, flags, width, height, cmtlen, dev, devlen, $
  173.     encoding, enclen, cbits
  174. endif
  175.  
  176. ; Write image:
  177. writeu, unit, image
  178.  
  179. ; Close file and free unit:
  180. free_lun, unit
  181.  
  182. end
  183.  
  184.